home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / answrbok / 6_5.lha / 6_5 / 6_5.h next >
Text File  |  1993-08-08  |  2KB  |  121 lines

  1. * Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
  2. * The C++ Answer Book */
  3. * Tony Hansen */
  4. * All rights reserved. */
  5. / Exercise 6.5
  6. / String class with value semantics
  7. / and delayed copying on assignment.
  8. ifndef STR_H
  9.  define STR_H
  10.  include <stream.h>
  11.  include <string.h>
  12.  
  13. include "6_5srep.h"    /* EXPAND */
  14.  
  15. / a helper typedef for the cast
  16. / back into a character pointer
  17. ypedef const char *charptr;
  18.  
  19. lass string
  20.  
  21.    srep *p;
  22.  
  23.    char *str()
  24.    { return p->s; }
  25.  
  26.    int len()
  27.    { return p->size; }
  28.  
  29. ublic:
  30.    // string s;
  31.    string()
  32.    { p = new srep; }
  33.  
  34.    // string s(5);
  35.    string(int sz)
  36.    { p = new srep(sz); }
  37.  
  38.    // string s("xyz");
  39.    string(char *s)
  40.    { p = new srep(s); }
  41.  
  42.    // string s = string ...
  43.    string(string &s)
  44.    { s.p->refcnt++; p = s.p; }
  45.  
  46.    ~string()
  47.    {
  48. if (--p->refcnt == 0)
  49.     delete p;
  50.    }
  51.  
  52.    // s = string
  53.    string& operator=(string &s)
  54.    {
  55. s.p->refcnt++;
  56. if (--p->refcnt == 0)
  57.     delete p;
  58. p = s.p;
  59. return *this;
  60.    }
  61.  
  62.    // s = "xyz";
  63.    string& operator=(char *s)
  64.    {
  65. if (p->refcnt > 1)
  66.     {
  67.     p->refcnt--;
  68.     p = new srep(s);
  69.     }
  70.  
  71. else
  72.     *p = s;
  73. return *this;
  74.    }
  75.  
  76.    // char *c = s;
  77.    const char *operator charptr()
  78.    {
  79. return str();
  80.    }
  81.  
  82.    // x = s[3];
  83.    // s[3] = 'x';
  84.    char &operator[](int i)
  85.    {
  86. if (i < 0 || i >= len())
  87.     return str()[0];
  88. else
  89.     return str()[i];
  90.    }
  91.  
  92.    friend ostream& operator<<(ostream&, string&);
  93.    friend istream& operator>>(istream&, string&);
  94.  
  95.    friend int operator==(string &x, char *s)
  96.    { return strcmp(x.str(), s) == 0; }
  97.  
  98.    friend int operator==(string &x, string &y)
  99.    { return strcmp(x.str(), y.str()) == 0; }
  100.  
  101.    friend int operator!=(string &x, char *s)
  102.    { return strcmp(x.str(), s) != 0; }
  103.  
  104.    friend int operator!=(string &x, string &y)
  105.    { return strcmp(x.str(), y.str()) != 0; }
  106. ;
  107.  
  108. nline ostream& operator<<(ostream &s, string &x)
  109.  
  110.    return s << x.str();
  111.  
  112.  
  113. nline istream& operator>>(istream &s, string &x)
  114.  
  115.    char buf[1024];
  116.    s >> buf;
  117.    x = buf;
  118.    return s;
  119.  
  120. endif /* STR_H */
  121.